package org.csu.forum.controller;

import org.csu.forum.common.CommonResponse;
import org.csu.forum.domain.PO.*;
import org.csu.forum.domain.VO.FileListVO;
import org.csu.forum.domain.VO.SetListVO;
import org.csu.forum.service.AccountService;
import org.csu.forum.service.SectionService;
import org.csu.forum.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SectionController {

    @Autowired
    SectionService sectionService;

    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    AccountService accountService;

    @PostMapping("/sections")
    @ResponseBody
    public CommonResponse<Section> addSection(@RequestBody Section section){
        if(isAdmin()){
            int sectionid = sectionService.addSection(section);
            if(sectionid == -1){
                return CommonResponse.createForError("已有一个同名的版块");
            }
            section.setSectionid(sectionid);
            return CommonResponse.createForSuccess(section,"添加版块成功");
        }
        return CommonResponse.createForError("添加版块失败");
    }

    @DeleteMapping("/sections/{sectionid}")
    @ResponseBody
    public CommonResponse<String> deleteSection(@PathVariable int sectionid){
        if(isAdmin()){
            sectionService.deleteSection(sectionid);
            return CommonResponse.createForSuccess("删除版块成功");
        }
        return CommonResponse.createForError("删除版块失败");
    }

    @PutMapping("/sections/{sectionid}")
    @ResponseBody
    public CommonResponse<Section> editSection(@RequestBody Section section){
        //管理员或版主可以修改版块描述
        if(isAdmin() || ismoderator(section.getSectionid())){
            sectionService.editSection(section);
            return CommonResponse.createForSuccess(section,"编辑版块成功");
        }
        return CommonResponse.createForError("编辑版块失败");
    }

    @GetMapping("/sections")
    @ResponseBody
    public CommonResponse<List<Section>> getSectionList(){
        return CommonResponse.createForSuccess(sectionService.getSectionList(),"获取所有版块成功");
    }

    @PostMapping("/sections/{sectionid}/zones")
    @ResponseBody
    public CommonResponse<Zone> addZoneToSection(@RequestBody Zone zone){
        if(isAdmin() || ismoderator(zone.getSectionid())){
            zone.setZoneid(sectionService.addZones(zone));
            return CommonResponse.createForSuccess(zone,"添加分区成功");
        }
        return CommonResponse.createForError("添加分区失败");
    }

    @GetMapping("/sections/{sectionid}/zones")
    @ResponseBody
    public CommonResponse<List<Zone>> getZoneList(@PathVariable int sectionid){
        return CommonResponse.createForSuccess(sectionService.getZoneList(sectionid),"获取分区成功");
    }

    @PutMapping("/sections/{sectionid}/zones/{zoneid}")
    @ResponseBody
    public CommonResponse<Zone> editZone(Zone zone){
        if(isAdmin() || ismoderator(zone.getSectionid())){
            sectionService.editZone(zone);
            return CommonResponse.createForSuccess(zone,"编辑分区成功");
        }
        return CommonResponse.createForError("编辑分区失败");
    }

    @DeleteMapping("/sections/{sectionid}/zones/{zoneid}")
    @ResponseBody
    public CommonResponse<String> deleteZone(@PathVariable int sectionid , @PathVariable int zoneid){
        if(isAdmin() || ismoderator(sectionid)){
            sectionService.deleteZone(zoneid);
            return CommonResponse.createForSuccess("删除分区成功");
        }
        return CommonResponse.createForError("删除分区失败");
    }

    @GetMapping("/sections/{sectionsid}/sectionowners")
    @ResponseBody
    public CommonResponse<Account> getSectionowners(@PathVariable int sectionsid){
        if(isAdmin()){
            return CommonResponse.createForSuccess(accountService.getAccount(sectionService.getSectionOwner(sectionsid)),"获取版块版主成功");
        }
        return CommonResponse.createForError("获取版块版主失败");
    }

    @PutMapping("/sections/{sectionid}/posts")
    @ResponseBody
    public CommonResponse<List<Titlepost>> handlePostList(@RequestBody SetListVO[] handleList, @PathVariable int sectionid,  String action){
        List<Titlepost>titlepostList = new ArrayList<>();
        if(isAdmin() || ismoderator(sectionid)){
            if(action.equals("setTop")){
                for(int i = 0; i < handleList.length ; i++){
                    SetListVO setListVO = handleList[i];
                    int postid = setListVO.getPostid();
                    Titlepost titlepost = sectionService.getTitlepost(postid);
                    titlepost.setIstop(true);
                    sectionService.editPostStatus(titlepost);
                    titlepostList.add(titlepost);
                }
                return CommonResponse.createForSuccess(titlepostList,"批量置顶成功");
            }else if(action.equals("cancelTop")){
                for(int i = 0; i < handleList.length ; i++){
                    SetListVO setListVO = handleList[i];
                    int postid = setListVO.getPostid();
                    Titlepost titlepost = sectionService.getTitlepost(postid);
                    titlepost.setIstop(false);
                    sectionService.editPostStatus(titlepost);
                    titlepostList.add(titlepost);
                }
                return CommonResponse.createForSuccess(titlepostList,"批量取消置顶成功");
            }else if(action.equals("setEssence")){
                for(int i = 0; i < handleList.length ; i++){
                    SetListVO setListVO = handleList[i];
                    int postid = setListVO.getPostid();
                    Titlepost titlepost = sectionService.getTitlepost(postid);
                    titlepost.setIsesscence(true);
                    sectionService.editPostStatus(titlepost);
                    titlepostList.add(titlepost);
                }
                return CommonResponse.createForSuccess(titlepostList,"批量加精成功");
            }else if(action.equals("cancelEssence")){
                for(int i = 0; i < handleList.length ; i++){
                    SetListVO setListVO = handleList[i];
                    int postid = setListVO.getPostid();
                    Titlepost titlepost = sectionService.getTitlepost(postid);
                    titlepost.setIsesscence(false);
                    sectionService.editPostStatus(titlepost);
                    titlepostList.add(titlepost);
                }
                return CommonResponse.createForSuccess(titlepostList,"批量取消加精成功");
            }
        }
        return CommonResponse.createForError("批量操作失败");
    }

    @DeleteMapping("/sections/{sectionid}/posts")
    @ResponseBody
    public CommonResponse<String> deletePostList(@RequestBody SetListVO[] handleList , @PathVariable int sectionid){
        if(isAdmin() || ismoderator(sectionid)){
            for(int i = 0 ; i < handleList.length ; i++){
                SetListVO setListVO = handleList[i];
                sectionService.deleteTitlePost(setListVO.getPostid());
            }
            CommonResponse.createForError("批量删除主题帖成功");
        }
        return CommonResponse.createForError("批量删除主题帖失败");
    }

    @GetMapping("/files")
    @ResponseBody
    public CommonResponse<List<Files>> getFileDescList(@RequestBody FileListVO[] handleList){
        List<Files> filesList = new ArrayList<>();
        if(isAdmin()){
            for(int i = 0 ; i < handleList.length ; i++){
                FileListVO fileListVO = handleList[i];
                filesList.add(sectionService.getFileSrc(fileListVO.getFileid()));
            }
            return CommonResponse.createForSuccess(filesList,"批量获取文件信息成功");
        }
        return CommonResponse.createForError("批量获取文件信息失败");
    }

    private Boolean isAdmin(){
        return 2 == TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"),"userid");
    }

    //是否为这个版块的版主
    private Boolean ismoderator(int sectionid){
        int userid = TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"),"userid");
        return accountService.getAccount(sectionService.getSectionOwner(sectionid)).getUserid() == userid;
    }
}
